﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ChoDiaOc_HK_WPF.Models;
using ChoDiaOc_HK_WPF.Controls;
using System.Data.Objects;
using System.Media;
using System.IO;
using Microsoft.Win32;

namespace ChoDiaOc_HK_WPF.Views.QuanLy {
    /// <summary>
    /// Interaction logic for QuanLyDiaOc.xaml
    /// </summary>
    public partial class QuanLyDiaOc : UserControl {
        private List<LOAIDIAOC> listLoaiDiaOc = null;

        public List<LOAIDIAOC> ListLoaiDiaOc {
            get {
                if (listLoaiDiaOc == null) {
                    listLoaiDiaOc = DatabaseWrapper.GetInstance().GetAllLoaiDiaOc();
                }
                return listLoaiDiaOc;
            }
            set { listLoaiDiaOc = value; }
        }
        public QuanLyDiaOc() {
            InitializeComponent();
            entities = new DBEntities();
            List<String> listTenLoaiDiaOc = ListLoaiDiaOc.Select(t => t.TenLoaiDiaOc).ToList();
            tenLoaiDiaOcComboBox.ItemsSource = listTenLoaiDiaOc;
        }
        private DBEntities entities;

        private void UserControl_Loaded(object sender, RoutedEventArgs e) {
            System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["dIAOCsViewSource"];
            myCollectionViewSource.Source = entities.DIAOCs;

        }

        private void btnXoa_Click(object sender, RoutedEventArgs e) {
            if (dIAOCsListView.SelectedIndex != -1) {
                DIAOC dn = dIAOCsListView.SelectedItem as DIAOC;

                if (MessageBoxResult.Yes == MessageBox.Show("Chắc chắn?", "Cảnh báo", MessageBoxButton.YesNo, MessageBoxImage.Question)) {
                    entities.DIAOCs.DeleteObject(dIAOCsListView.SelectedItem as DIAOC);
                    entities.SaveChanges();
                }
            }
        }

        private void btnHoanTat_Click(object sender, RoutedEventArgs e) {
            try {
                entities.SaveChanges();
                MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            } catch (Exception exp) {
                MessageBox.Show(exp.Message);
            }

        }

        private void btnNhapLai_Click(object sender, RoutedEventArgs e) {
            System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["dIAOCsViewSource"];
            entities = new DBEntities();
            myCollectionViewSource.Source = entities.DIAOCs;

        }

        private void tenLoaiDiaOcComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            if (tenLoaiDiaOcComboBox.SelectedIndex != -1) {
                if (dIAOCsListView.SelectedIndex != -1) {
                    DIAOC diaOc = dIAOCsListView.SelectedItem as DIAOC;
                    String selected = (sender as ComboBox).SelectedItem as String;
                    LOAIDIAOC loaiDiaOc = entities.LOAIDIAOCs.Where(loai => loai.TenLoaiDiaOc == selected).Single();
                    diaOc.MaLoaiDiaOc = loaiDiaOc.MaLoaiDiaOc;
                    kyHieuTextBlock.Text = loaiDiaOc.KyHieu;
                }
            }
        }

        private NOIDUNGQC noiDungQC;
        public ChoDiaOc_HK_WPF.NOIDUNGQC NoiDungQC {
            get {
                if (noiDungQC == null) {
                    noiDungQC = new NOIDUNGQC();
                }
                return noiDungQC;
            }
            set { noiDungQC = value; }
        }

        private void dIAOCsListView_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            if (dIAOCsListView.SelectedIndex != -1) {
                imgAnhChup.Source = null;
                DIAOC diaOc = dIAOCsListView.SelectedItem as DIAOC;
                PHIEUDANGKY phieu = diaOc.PHIEUDANGKies.FirstOrDefault();
                QC_BAO qcBao = phieu.QC_BAO.FirstOrDefault();
                QC_BANG qcBang = phieu.QC_BANG.FirstOrDefault();
                TOBUOM qcBuom = phieu.TOBUOMs.FirstOrDefault();
                NOIDUNGQC noiDung = null;
                if (qcBang != null) {
                    noiDung = qcBang.NOIDUNGQC;
                } else {
                    if (qcBao != null) {
                        noiDung = qcBao.NOIDUNGQC;
                    } else {
                        if (qcBuom != null) {
                            noiDung = qcBuom.NOIDUNGQC;
                        }
                    }
                }
                if (noiDung != null) {
                    NoiDungQC.MaND = noiDung.MaND;
                }

                System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["hINHANHsViewSource"];
                myCollectionViewSource.Source = entities.HINHANHs.Where(ha => ha.MaND == NoiDungQC.MaND);

            }
        }

        private void hINHANHsListView_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            if (hINHANHsListView.SelectedIndex != -1) {
                HINHANH ha = hINHANHsListView.SelectedItem as HINHANH;
                FileStream stream = new FileStream("./AnhChupDiaOc/" + ha.Anh, FileMode.Open, FileAccess.Read);
                Image i = new Image();
                BitmapImage src = new BitmapImage();
                src.BeginInit();
                src.StreamSource = stream;
                src.EndInit();
                imgAnhChup.Source = src;
                imgAnhChup.Stretch = Stretch.Fill;
            }
        }

        private void btnThem_Click(object sender, RoutedEventArgs e) {
            if (dIAOCsListView.SelectedIndex != -1) {
                if (entities.HINHANHs.Where(ha32 => ha32.MaND == NoiDungQC.MaND).ToList().Count == DatabaseWrapper.GetInstance().GetThamSoSoLuongAnh()) {
                    MessageBox.Show("Đã đủ ảnh theo quy định!", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
                    return;
                }
                HINHANH ha = new HINHANH();
                ha.MaHinhAnh = ChupAnhController.GetInstance().GenerateMaHINHANH();
                OpenFileDialog filedialog = new OpenFileDialog();
                filedialog.Filter = "*.png|*.png|*.jpg|*.jpg|*.bmp|*.bmp|*.ico|*.ico";
                filedialog.ShowDialog();
                FileInfo fi;
                if (filedialog.FileName != String.Empty) {
                    fi = new FileInfo(filedialog.FileName);
                    if (fi.Exists) {
                        ha.Anh = fi.Name;
                        ha.MaND = NoiDungQC.MaND;
                        ha.MoTa = "Ảnh chụp địa ốc";
                        ha.ThoiGianChupAnh = DateTime.Now;
                        FileInfo fiEx = new FileInfo("./AnhChupDiaOc/" + fi.Name);
                        if (fiEx.Exists) {
                            return;
                        }
                        fi.CopyTo("./AnhChupDiaOc/" + fi.Name, true);
                        entities.HINHANHs.AddObject(ha);
                        entities.SaveChanges();
                        FileStream stream = new FileStream("./AnhChupDiaOc/" + ha.Anh, FileMode.Open, FileAccess.Read);
                        Image i = new Image();
                        BitmapImage src = new BitmapImage();
                        src.BeginInit();
                        src.StreamSource = stream;
                        src.EndInit();
                        imgAnhChup.Source = src;
                        imgAnhChup.Stretch = Stretch.Fill;
                        System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["hINHANHsViewSource"];
                        entities = new DBEntities();
                        myCollectionViewSource.Source = entities.HINHANHs.Where(ha2 => ha2.MaND == NoiDungQC.MaND);

                    }
                }
            }
        }

        private void btnXoaAnh_Click(object sender, RoutedEventArgs e) {
            if (hINHANHsListView.SelectedIndex != -1) {
                HINHANH ha = hINHANHsListView.SelectedItem as HINHANH;
                if (MessageBoxResult.Yes == MessageBox.Show("Chắc chắn?", "Cảnh báo", MessageBoxButton.YesNo, MessageBoxImage.Question)) {
                    entities.HINHANHs.DeleteObject(ha);
                    entities.SaveChanges();
                    imgAnhChup.Source = null;
                    hINHANHsListView.SelectedIndex = -1;
                }
            }
        }

        private void btnThayAnh_Click(object sender, RoutedEventArgs e) {
            if (hINHANHsListView.SelectedIndex != -1) {
                HINHANH ha = hINHANHsListView.SelectedItem as HINHANH;
                OpenFileDialog filedialog = new OpenFileDialog();
                filedialog.Filter = "*.png|*.png|*.jpg|*.jpg|*.bmp|*.bmp|*.ico|*.ico";
                filedialog.ShowDialog();
                FileInfo fi;
                if (filedialog.FileName != String.Empty) {
                    fi = new FileInfo(filedialog.FileName);
                    if (fi.Exists) {
                        //FileInfo fiEx = new FileInfo("./AnhChupDiaOc/" + ha.Anh);
                        //fiEx.Delete();
                        fi.CopyTo("./AnhChupDiaOc/" + fi.Name, true);
                        ha.Anh = fi.Name;
                        entities.SaveChanges();
                        FileStream stream = new FileStream("./AnhChupDiaOc/" + ha.Anh, FileMode.Open, FileAccess.Read);
                        Image i = new Image();
                        BitmapImage src = new BitmapImage();
                        src.BeginInit();
                        src.StreamSource = stream;
                        src.EndInit();
                        imgAnhChup.Source = src;
                        imgAnhChup.Stretch = Stretch.Fill;
                    }
                }
            }
        }
    }
}